<template>
  <div class="editor-tabs" :style="{ backgroundColor: props.backgroundColor }">
    <div class="tabs-header" :class="{ 'tabs-centered': props.centered }">
      <div 
        v-for="(tab, index) in props.tabs" 
        :key="index" 
        class="tab-item"
        :class="[
          { 'active': index === activeTab },
          `tab-type-${props.type}`
        ]"
        :style="getTabStyle(index)"
        @click="activeTab = index"
      >
        {{ tab.label }}
      </div>
    </div>
    <div class="tabs-content">
      <div 
        v-for="(tab, index) in props.tabs" 
        :key="index" 
        v-show="index === activeTab" 
        class="tab-pane"
        @dragover.prevent
        @drop="onDropComponent($event, index)"
      >
        <div v-if="!tab.components || tab.components.length === 0" class="tab-empty-tip">
          从左侧拖拽组件到此处
        </div>
        <template v-else>
          <div 
            v-for="(element, elementIndex) in tab.components" 
            :key="element.id"
            class="editor-tab-element"
            :class="{ 'element-selected': selectedElementIndex === elementIndex && selectedTabIndex === index }"
            @click.stop="selectElement(index, elementIndex)"
            draggable="true"
            @dragstart="elementDragStart($event, index, elementIndex)"
            @dragover.prevent
            @drop="elementDrop($event, index, elementIndex)"
          >
            <component 
              :is="element.component" 
              v-bind="element.props"
              class="element-component"
            ></component>
            <div class="element-actions" v-if="selectedElementIndex === elementIndex && selectedTabIndex === index">
              <button class="element-action delete" @click.stop="removeElement(index, elementIndex)" title="删除组件">
                <i class="minus-icon">-</i>
              </button>
            </div>
            <div class="drag-indicator" v-if="selectedElementIndex === elementIndex && selectedTabIndex === index">
              <i class="el-icon-rank"></i>
            </div>
          </div>
        </template>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineProps, ref, computed, defineEmits } from 'vue';

const props = defineProps({
  tabs: {
    type: Array,
    default: () => [
      { label: '选项一', content: '选项一内容', components: [] },
      { label: '选项二', content: '选项二内容', components: [] },
      { label: '选项三', content: '选项三内容', components: [] }
    ]
  },
  activeColor: {
    type: String,
    default: '#8B4513'
  },
  inactiveColor: {
    type: String,
    default: '#666666'
  },
  backgroundColor: {
    type: String,
    default: '#ffffff'
  },
  type: {
    type: String,
    default: 'line'
  },
  centered: {
    type: Boolean,
    default: false
  }
});

const emit = defineEmits(['tabComponentAdded', 'tabComponentRemoved', 'tabComponentSelected', 'tabComponentMoved']);

// 当前活跃的选项卡
const activeTab = ref(0);
// 当前选中的元素索引
const selectedElementIndex = ref<number | null>(null);
// 当前选中的选项卡索引
const selectedTabIndex = ref<number | null>(null);

// 获取选项卡样式
const getTabStyle = (index) => {
  const isActive = index === activeTab.value;
  
  return {
    color: isActive ? props.activeColor : props.inactiveColor,
    borderColor: isActive && props.type === 'line' ? props.activeColor : 'transparent',
    backgroundColor: isActive && props.type === 'card' ? props.activeColor : 'transparent',
  };
};

// 处理组件放置
const onDropComponent = (event, tabIndex) => {
  const type = event.dataTransfer.getData('type');
  
  if (type === 'new') {
    // 通知父组件添加新组件到选项卡
    const componentId = event.dataTransfer.getData('componentId');
    emit('tabComponentAdded', { tabIndex, componentId, position: 'end' });
    // 标记一个自定义属性，表示事件已被处理
    event.handled = true;
    // 阻止事件冒泡
    event.stopPropagation();
  } else if (type === 'reorder' && event.dataTransfer.getData('fromTabIndex')) {
    // 在选项卡之间移动组件
    const fromTabIndex = parseInt(event.dataTransfer.getData('fromTabIndex'));
    const fromElementIndex = parseInt(event.dataTransfer.getData('elementIndex'));
    
    if (!isNaN(fromTabIndex) && !isNaN(fromElementIndex)) {
      emit('tabComponentMoved', { 
        fromTabIndex, 
        fromElementIndex, 
        toTabIndex: tabIndex, 
        toElementIndex: props.tabs[tabIndex].components ? props.tabs[tabIndex].components.length : 0 
      });
      // 标记一个自定义属性，表示事件已被处理
      event.handled = true;
      // 阻止事件冒泡
      event.stopPropagation();
    }
  }
};

// 处理元素拖拽开始
const elementDragStart = (event, tabIndex, elementIndex) => {
  event.dataTransfer.setData('elementIndex', elementIndex);
  event.dataTransfer.setData('fromTabIndex', tabIndex);
  event.dataTransfer.setData('type', 'reorder');
};

// 处理元素放置（排序）
const elementDrop = (event, tabIndex, elementIndex) => {
  const type = event.dataTransfer.getData('type');
  
  if (type === 'reorder') {
    const fromTabIndex = parseInt(event.dataTransfer.getData('fromTabIndex'));
    const fromElementIndex = parseInt(event.dataTransfer.getData('elementIndex'));
    
    if (!isNaN(fromTabIndex) && !isNaN(fromElementIndex)) {
      // 通知父组件移动组件
      emit('tabComponentMoved', { 
        fromTabIndex, 
        fromElementIndex, 
        toTabIndex: tabIndex, 
        toElementIndex: elementIndex 
      });
    }
  } else if (type === 'new') {
    // 通知父组件添加新组件到特定位置
    const componentId = event.dataTransfer.getData('componentId');
    emit('tabComponentAdded', { tabIndex, componentId, position: 'specific', elementIndex });
    // 标记一个自定义属性，表示事件已被处理
    event.handled = true;
  }
  
  // 阻止事件冒泡
  event.stopPropagation();
};

// 选择元素
const selectElement = (tabIndex, elementIndex) => {
  selectedTabIndex.value = tabIndex;
  selectedElementIndex.value = elementIndex;
  emit('tabComponentSelected', { tabIndex, elementIndex });
};

// 删除元素
const removeElement = (tabIndex, elementIndex) => {
  emit('tabComponentRemoved', { tabIndex, elementIndex });
  selectedElementIndex.value = null;
  selectedTabIndex.value = null;
};

defineOptions({
  name: 'editor-tabs'
});
</script>

<style lang="scss" scoped>
.editor-tabs {
  width: 100%;
  
  .tabs-header {
    display: flex;
    border-bottom: 1px solid #e0e0e0;
    
    &.tabs-centered {
      justify-content: center;
    }
    
    .tab-item {
      padding: 10px 15px;
      cursor: pointer;
      position: relative;
      transition: all 0.3s;
      font-size: 14px;
      
      &.tab-type-line {
        &.active {
          border-bottom: 2px solid;
          margin-bottom: -1px;
        }
      }
      
      &.tab-type-card {
        margin: 5px 5px 0;
        border-radius: 4px 4px 0 0;
        
        &.active {
          color: #fff;
        }
      }
    }
  }
  
  .tabs-content {
    padding: 15px 0;
    
    .tab-pane {
      font-size: 14px;
      min-height: 100px;
      border: 1px dashed transparent;
      transition: all 0.3s;
      padding: 15px;
      padding-right: 25px;
      
      &:hover {
        border-color: #ddd;
      }
    }
    
    .tab-empty-tip {
      color: #aaa;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 80px;
      border: 2px dashed #eee;
      border-radius: 6px;
    }
    
    .editor-tab-element {
      position: relative;
      margin-bottom: 15px;
      margin-top: 15px;
      transition: all 0.3s ease;
      border: 2px solid transparent;
      border-radius: 8px;
      width: 100%;
      
      &:hover {
        box-shadow: 0 0 0 1px rgba(139, 69, 19, 0.2);
      }
      
      &.element-selected {
        border-color: #8B4513;
      }
      
      .element-component {
        width: 100%;
        min-height: 30px;
      }
      
      .element-actions {
        position: absolute;
        top: -15px;
        right: 10px;
        display: flex;
        gap: 5px;
        z-index: 10;
        
        .element-action {
          width: 28px;
          height: 28px;
          border-radius: 50%;
          border: none;
          display: flex;
          justify-content: center;
          align-items: center;
          cursor: pointer;
          color: #fff;
          transition: all 0.3s ease;
          box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
          
          &.delete {
            background-color: #ff4d4f;
            
            &:hover {
              background-color: #ff7875;
              transform: scale(1.15);
            }
            
            &:active {
              background-color: #cf1322;
              transform: scale(1);
            }
          }
          
          .minus-icon {
            font-size: 20px;
            line-height: 1;
            font-weight: bold;
          }
        }
      }
      
      .drag-indicator {
        position: absolute;
        top: calc(50% - 12px);
        left: -15px;
        width: 24px;
        height: 24px;
        border-radius: 50%;
        background-color: rgba(139, 69, 19, 0.8);
        color: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: move;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
        opacity: 0.7;
        transition: opacity 0.3s ease;
        
        &:hover {
          opacity: 1;
        }
      }
    }
  }
  
  &:hover {
    outline: 1px dashed #ccc;
  }
}
</style> 